﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using EntityTools.Model;
using WAF.EntityFramework;

namespace EntityTools.Forms
{
    public partial class FieldInfos : Form
    {
        private MainForm mainForm;
        


        public FieldInfos(MainForm form)
        {
            InitializeComponent();

            mainForm = form;
        }


        /**************************** Private *****************************/

        private void LoadRefTableList()
        {
            string dbid = mainForm.GetSelectedDbNode().Name;
            TableInfo[] tbInfos = TableInfo.GetTableList(dbid);

            comboBoxRefTable.DataSource = tbInfos;
            comboBoxRefTable.DisplayMember = "TableName";
            comboBoxRefTable.ValueMember = "Id";
        }

        private void LoadFieldType()
        {
            string[] fieldType = { "String", "Int32", "Int64", "Double" };

            comboBoxFieldType.DataSource = fieldType;
        }


        /**************************** 窗体事件 ****************************/

        private void FieldInfos_Load(object sender, EventArgs e)
        {
            LoadFieldType();
            LoadRefTableList();

            string dbName = mainForm.GetSelectedDbNode().Text;
            string tbName = mainForm.GetTreeView().SelectedNode.Text;

            this.Text = dbName + "." + tbName + "的字段信息";
        }

        private void comboBoxRefTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            string tbID = ((TableInfo)comboBoxRefTable.SelectedItem).Id.ToString();

            comboBoxRefField.Items.Clear();
            comboBoxRefField.BeginUpdate();

            foreach (FieldInfo field in FieldInfo.GetFieldList(tbID))
            { 
                comboBoxRefField.Items.Add(field.FieldName + "(" + field.Label + ")");
            }

            comboBoxRefField.EndUpdate();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            FieldInfo field = new FieldInfo();

            field.TableID = long.Parse(mainForm.GetTreeView().SelectedNode.Name);
            field.FieldName = txtFieldName.Text.Trim();
            field.FieldType = "DbType." + comboBoxFieldType.Text;
            field.Label = txtLabel.Text.Trim();
            field.RefTable = comboBoxRefTable.Text;
            field.RefLableField = comboBoxRefField.SelectedItem == null ? "" : comboBoxRefField.Text;
            field.Remark = txtRemark.Text.Trim();

            field.IsPrimaryKey  = false;
            field.IsDisplay     = checkBoxIsDisplay.Checked;
            field.IsForeignKey  = checkBoxIsForeignKey.Checked;
            field.IsNullable    = checkBoxIsNullable.Checked;
            field.IsUnique      = checkBoxIsUnique.Checked;

            EntityAction.Insert(field);
            this.Close();
        }


    }
}
